package com.neutech.dao.impl;

import com.neutech.constant.Constant;
import com.neutech.dao.BusinessDAO;
import com.neutech.entity.Business;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BusinessDAOImpl implements BusinessDAO {

    @Override
    public List<Business> listBusiness() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<Business> businessList = new ArrayList<>();

        try {
            connection = Constant.DATASOURCE.getConnection();
            preparedStatement = connection.prepareStatement("select * from business");
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                Business business = new Business();
                business.setBusinessId(resultSet.getInt("businessId"));
                business.setBusinessName(resultSet.getString("businessName"));
                business.setBusinessAddress(resultSet.getString("businessAddress"));
                business.setBusinessExplain(resultSet.getString("businessExplain"));
                business.setBusinessImg(resultSet.getString("businessImg"));
                business.setMonth_sales(resultSet.getInt("month_sales"));
                business.setStarPrice(resultSet.getBigDecimal("starPrice"));
                business.setDeliveryPrice(resultSet.getBigDecimal("deliveryPrice"));

                businessList.add(business);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return businessList;
    }

    @Override
    public Business getById(Integer id) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        Business business = null;

        try {
            connection = Constant.DATASOURCE.getConnection();
            String sql = "select * from business where businessid = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,id);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                business = new Business();
                business.setBusinessId(resultSet.getInt("businessId"));
                business.setBusinessName(resultSet.getString("businessName"));
                business.setBusinessAddress(resultSet.getString("businessAddress"));
                business.setBusinessExplain(resultSet.getString("businessExplain"));
                business.setBusinessImg(resultSet.getString("businessImg"));
                business.setMonth_sales(resultSet.getInt("month_sales"));
                business.setStarPrice(resultSet.getBigDecimal("starPrice"));
                business.setDeliveryPrice(resultSet.getBigDecimal("deliveryPrice"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }

        return business;
    }

    @Override
    public Business searchByName(String name) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        Business business = null;

        try {
            connection = Constant.DATASOURCE.getConnection();
            String sql = "select * from business where businessName = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,name);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                business = new Business();
                business.setBusinessId(resultSet.getInt("businessId"));
                business.setBusinessName(resultSet.getString("businessName"));
                business.setBusinessAddress(resultSet.getString("businessAddress"));
                business.setBusinessExplain(resultSet.getString("businessExplain"));
                business.setBusinessImg(resultSet.getString("businessImg"));
                business.setMonth_sales(resultSet.getInt("month_sales"));
                business.setStarPrice(resultSet.getBigDecimal("starPrice"));
                business.setDeliveryPrice(resultSet.getBigDecimal("deliveryPrice"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }

        return business;
    }


}
